package com.chagee.channel.api.bo.lark;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.chagee.channel.api.enums.LarkApprovalTaskEventStatusEnum;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 审批实例状态改变回调参数
 *
 * @author: chang.jiawei
 * @date: 2024/01/18
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ApprovalTaskStatusChangeCallbackBO implements Serializable {

    private static final long serialVersionUID = 7572389507846226037L;
    private String userId;

    @ApiModelProperty("审批实例任务状态变更：approval_task")
    private String type;

    private String approvalCode;

    private String instanceCode;

    private String taskId;

    private String defKey;

    private LarkApprovalTaskEventStatusEnum status;

    private LocalDateTime operateTime;

    /**
     * 回退信息
     * @see com.chagee.channel.api.bo.lark.ApprovalTaskStatusChangeCallbackBO.RollbackDetail
     */
    private String extra;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class RollbackDetail{
        private List<String> rollbackNodeIds;

        private List<String> rollbackCustomNodeIds;
    }

    public static class LarkApprovalExtraDeserializer implements ObjectDeserializer {
        @SuppressWarnings("unchecked")
        @Override
        public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            String extra = parser.parseObject(type);
            if (Objects.isNull(extra)) {
                return null;
            }
            return (T) JSONObject.parseObject(extra, RollbackDetail.class);
        }
        @Override
        public int getFastMatchToken() {
            return JSONToken.LITERAL_STRING;
        }
    }

}
